package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@SuppressWarnings("all")
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierDao supplierDao;
    @Override
    public Map<String, Object> list(Integer page, Integer rows, String supplierName) {
        List<Supplier>  list=supplierDao.list((page-1)*rows,rows,supplierName);
        Map<String, Object> map=new HashMap<>();
        map.put("rows",list);
        map.put("total",list.size());
        return map;
    }

    @Override
    public void save(Supplier supplier) {
        if(StringUtils.isEmpty(supplier.getSupplierId())){
            //增加
            supplierDao.insert(supplier);
            return;
        }else {
            //修改+
            supplierDao.update(supplier,supplier.getSupplierId());
            return;
        }
    }

    /**
     * 添加修改
     * @param supplier
     * @param supplierid
     */
/*    @Override
    public void save(Supplier supplier) {
        if(StringUtils.isEmpty(supplier)){
            //增加
            supplierDao.insert(supplier);
            return;
        }else {
            //修改+
            supplierDao.update(supplier,supplierid);
            return;
        }
    }*/
    //删除
    @Override
    public void delete(String ids) {
        String[] split = ids.split(",");
        supplierDao.delete(Arrays.asList(split));
    }
}
